<template>
	<!-- 上传图片 -->
	<u-upload class="l-upload" :width="width" :height="height" :fileList="fileCList" @afterRead="afterRead"
		@delete="deletePic" name="1" multiple :maxCount="maxCount"></u-upload>

</template>

<script>
	export default {
		props: {
			fileList: {
				default: [],
				type: Array
			},
			// 最大条数
			maxCount: {
				default: 10,
				type: Number
			},
			width: {
				default: 120,
				type: Number
			},
			height: {
				default: 120,
				type: Number
			}
		},
		data() {
			return {

			}
		},
		computed: {
			fileCList() {
				return this.fileList || []
			}
		},
		methods: {
			// deletePic(e){
			// 	this.$emit('delete',e)
			// },
			// 删除图片
			deletePic(event) {
				this.fileCList.splice(event.index, 1)
				this.$emit('update:fileList', this.fileCList)
			},
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				console.log(event)
				let fileListLen = this.fileCList.length
				lists.map((item) => {
					this.fileCList.push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
				
					let result = await this.$Net.upload(lists[i].url)
					
					if (result.success) {
						let item = this.fileCList[fileListLen]
						this.fileCList.splice(fileListLen, 1, Object.assign(item, {
							status: 'success',
							message: '',
							url: result
						}))
					} else {
						let item = this.fileCList[fileListLen]
						this.fileCList.splice(fileListLen, 1, Object.assign(item, {
							status: 'failed',
							message: '',
							url: result
						}))
					}

					fileListLen++
				}
				console.log(this.fileCList)
				this.$emit('update:fileList', this.fileCList)

			},
		}
	}
</script>
<style scoped lang="scss">
	::v-deep .u-upload {
		.u-upload__deletable {
			width: 36rpx !important;
			height: 36rpx !important;
		}

		.uicon-camera-fill {
			font-size: 36rpx !important;
			line-height: 36rpx !important;
		}

		.u-upload__status__message {
			font-size: 20rpx !important;
		}
	}
</style>
